package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.bean.Comment;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.ProductEngine;

public class ProductEngineImpl extends BaseEngine implements ProductEngine {

	@Override
	public Product getProductDetailInfo(int pId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pId", pId);
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_PRODUCT_DETAIL_URI, map);
		Product product = JSONObject.parseObject(json.getString("product"), Product.class);
		return product;
	}

	@Override
	public Map<String, Object> getProductComment(int pId, int page, int pageNum) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pId", pId);
		map.put("page", page);
		map.put("pageNum", pageNum);
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_PRODUCT_COMMENT, map);
		List<Comment> list_comment = JSONObject.parseArray(json.getString("comment"), Comment.class);
		Integer comment_number = json.getInteger("list_count");
		
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("comments", list_comment);
		resMap.put("comment_number", comment_number);
		return resMap;
	}

	@Override
	public boolean addFavorite(int pId, int userid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pId", pId);
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_ADDFAV_URI, map, "" + userid);
		Boolean addSuccess = json.getBoolean("result");
		
		return addSuccess;
	}

	@Override
	public String getProductDesc(int pId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pId", pId);
		JSONObject json = getJson(TYPE_GET, ContantValue.REDBOY_PRODUCT_DESC, map);
		String desc = json.getString("productdesc");
		return desc;
	}
}
